#include<opencv2/opencv.hpp>
#include<iostream>

using namespace cv;
using namespace std;

int main()
{
    system("color F0");
    Mat img=imread("/home/lwj/opencv_projeci/wendideguozhao.jpg");
    Size newSize(300,320);
    Mat imgs;
    resize(img,imgs,newSize);
    Mat image;
    cvtColor(imgs,image,COLOR_BGR2GRAY);//灰度
    int histSize=256;//直方图尺寸
    const int channels[1]={0};//索引通道
    float range[2]={0,255};//像素灰度值
    const float*histRange={range};
    Mat hist;
    calcHist(&image,1,channels,Mat (),hist,1,&histSize,&histRange);//计算直方图数据
    int hist_w=512;
    int hist_h=400;
    int bin_W=cvRound((double)hist_w/histSize);//白色矩形的宽
    Mat histImage=Mat::zeros(hist_h,hist_w,CV_8UC3);
   // Mat hist_l1;
    normalize(hist,hist,0,10000,NORM_MINMAX);
    for(int i=1;i<=hist.rows;i++)
    {
       rectangle(histImage,Point(bin_W*(i-1),hist_h-1),Point(bin_W*i-1,
       hist_h-cvRound(hist.at<float>(i-1)/15)),Scalar(255,255,255),-1);
    }
    namedWindow("histImage",WINDOW_NORMAL);
    imshow("histImage",histImage);
    //  namedWindow("原图",WINDOW_NORMAL);
    // imshow("原图",imgs);
    //  namedWindow("灰度图",WINDOW_NORMAL);
    // imshow("灰度图",image);
    waitKey(0);
    return 0;
}
